import time

from selenium.webdriver.common.by import By
from common.TestTool import BlogDriver
from selenium.webdriver.support import expected_conditions as EC

class BlogLogin:
    driver = ""
    url = ""
    def __init__(self):
        self.driver = BlogDriver
        self.url = "http://8.137.19.140:9090/blog_login.html"
        self.driver.get(self.url)
    def login_to_success(self):
        print(f"当前url:{self.url}")
        #先定位表单元素
        elem_usr = self.driver.find_by_select("#username")
        self.driver.emulate_key_inputs(elem_usr,"zhangsan")
        elem_pwd = self.driver.find_by_select("#password")
        self.driver.emulate_key_inputs(elem_pwd, "123456")
        self.driver.log_to_image("login_to_success")
        #模拟点击事件
        submit = self.driver.find_by_select("#submit")
        self.driver.click(submit)
        #显示等待
        self.driver.display_wait(3,EC.visibility_of_element_located((By.XPATH,'/html/body/div[2]/div[1]/div/h3')))
        self.driver.display_wait(3,EC.visibility_of_element_located((By.XPATH,'/html/body/div[2]/div[1]/div/a')))
        self.driver.display_wait(3, EC.visibility_of_element_located(
            (By.XPATH, '/html/body/div[2]/div[1]/div/div[2]/span[1]')))
        self.driver.display_wait(3, EC.visibility_of_element_located(
            (By.XPATH, '/html/body/div[2]/div[1]/div/div[2]/span[2]')))
        username = self.driver.find_by_xpath('/html/body/div[2]/div[1]/div/h3').text
        github = self.driver.find_by_xpath('/html/body/div[2]/div[1]/div/a').text
        cnt = self.driver.find_by_xpath('/html/body/div[2]/div[1]/div/div[2]/span[1]').text
        kinds = self.driver.find_by_xpath('/html/body/div[2]/div[1]/div/div[2]/span[2]').text
        print(f"用户名:{username}")
        print(f"github:{github}")
        print(f"文章数量:{cnt}")
        print(f"分类:{kinds}")
        self.driver.log_to_image("login_to_success")
    def login_to_error(self):
        print(f"当前url:{self.url}")
        # 先定位表单元素
        elem_usr = self.driver.find_by_select("#username")
        self.driver.emulate_key_inputs(elem_usr, "lhd")
        elem_pwd = self.driver.find_by_select("#password")
        self.driver.emulate_key_inputs(elem_pwd, "123456")
        # 模拟点击事件
        submit = self.driver.find_by_select("#submit")
        self.driver.log_to_image("login_to_error")
        self.driver.click(submit)
        self.driver.display_wait(2,EC.alert_is_present())
        print(f"alert: {self.driver.text_from_alert()}")
        self.driver.accept_to_alert()
# def main():
#     login = BlogLogin()
#     login.login_to_success()
#     login.login_to_error()
# main()